package rs.udd.service.mappings;

import rs.udd.model.AdvertiserAgentContact;
import rs.udd.model.Agent;
import rs.udd.model.AgentAdvertApplication;
import rs.udd.service.dto.agent.AgentEditDto;
import rs.udd.service.dto.agent.AgentRegisterDto;
import rs.udd.service.dto.agent.AgentViewCvDto;
import rs.udd.service.dto.agent.AgentViewDto;
import rs.udd.service.dto.user.UserPasswordEditDto;

public class AgentMapping {

	// this private constructor prevents this class from instantiation
	private AgentMapping() {
	}

	public static Agent register(final AgentRegisterDto dto) {

		Agent agent = new Agent();

		agent.setActive(true); // agent is activated as soon as registered
		agent.setBirthDate(dto.getBirthDate());
		agent.setEmail(dto.getEmail());
		agent.setEmailActive(true);
		agent.setFirstname(dto.getFirstname());
		// agent.setFolderWithCv(dto.getFolderWithCv().getOriginalFilename()); // TODO Nikola - set location of uploaded file on server
		agent.setGender(dto.getGender());
		agent.setLastname(dto.getLastname());
		agent.setPassword(dto.getPassword());
		agent.setUsername(dto.getUsername());

		return agent;
	}

	public static AgentEditDto edit(final Agent agent) {

		AgentEditDto dto = new AgentEditDto();
		// TODO

		return dto;
	}

	public static void edit(final AgentEditDto dto, Agent agent) {

		agent.setBirthDate(dto.getBirthDate());
		agent.setEmail(dto.getEmail());
		agent.setFirstname(dto.getFirstname());
		agent.setGender(dto.getGender());
		agent.setLastname(dto.getLastname());
	}

	public static void editPassword(final UserPasswordEditDto dto, Agent agent) {

		agent.setPassword(dto.getNewPassword());
	}

	public static AgentViewDto view(final Agent agent) {

		AgentViewDto dto = new AgentViewDto();

		dto.setId(agent.getId());
		dto.setUsername(agent.getUsername());
		dto.setFirstname(agent.getFirstname());
		dto.setLastname(agent.getLastname());
		dto.setEmail(agent.getEmail());
		dto.setCreationTime(agent.getCreationTime());
		dto.setModificationTime(agent.getModificationTime());
		dto.setBirthDate(agent.getBirthDate());
		dto.setGender(agent.getGender());
		dto.setFolderWithCv(agent.getFolderWithCv());
		for (AdvertiserAgentContact aac : agent.getContactedByAdvertiser())
			dto.getContactedByAdvertiser().add(aac.getAdvertiser());
		for (AgentAdvertApplication application : agent.getApplicationsForAdvert())
			dto.getApplicationsForAdvert().add(application.getAdvert());
		dto.setIndexedDocumentId(agent.getIndexedDocumentId());

		return dto;
	}
	
	public static AgentViewDto viewToEdit(final Agent agent) {

		AgentViewDto dto = new AgentViewDto();

		dto.setId(agent.getId());
		dto.setFirstname(agent.getFirstname());
		dto.setLastname(agent.getLastname());
		dto.setEmail(agent.getEmail());
		dto.setBirthDate(agent.getBirthDate());
		dto.setGender(agent.getGender());

		return dto;
	}

	public static AgentViewCvDto viewCv(final Agent agent) {
		AgentViewCvDto dto = new AgentViewCvDto();
		dto.setId(agent.getId());
		dto.setFolderWithCv(agent.getFolderWithCv());

		return dto;
	}

	public static UserPasswordEditDto editPassword(final Agent agent) {

		UserPasswordEditDto dto = new UserPasswordEditDto();

		dto.setId(agent.getId());

		return dto;
	}

}
